package company

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"jwgin/model/mysql"
	"jwgin/service"
)

//用户金豆变更
type recordBean struct {
}

func (l *recordBean) Routers(Router *gin.RouterGroup) (R gin.IRoutes) {
	Router.GET("record/bean", l.get)
	Router.GET("record/bean/user", l.getUser)
	return Router
}

//获取所有列表
func (recordBean) get(c *gin.Context) {
	count, size, offset := pages(c)
	companyid := c.GetUint("UserID")
	if companyid == 0 {
		RespErr(c, fmt.Errorf("系统用户认证失败"))
		return
	}

	db := service.G.Model(mysql.RecordBean{}).Joins("JOIN `user` ON user.id = record_bean.uid").Where("comid = ?", companyid)

	if uid := c.Query("uid"); uid != "" {
		db = db.Where("uid = ?", uid)
	}

	if tp := c.Query("type"); tp != "" {
		db = db.Where("type = ?", tp)
	}

	//时间筛选
	startTime := c.Query("startTime")
	endTime := c.Query("endTime")
	if startTime != "" && endTime != "" {
		db = db.Where(" record_bean.addtime > ? AND record_bean.addtime < ? ", startTime, endTime)
	}

	list := []map[string]interface{}{}
	db.Select("user.account,record_bean.*").Count(&count).Offset(offset).Limit(size).Scan(&list)
	RespList(c, count, list)
}
func (recordBean) getUser(c *gin.Context) {

	companyid := c.GetUint("UserID")
	if companyid == 0 {
		RespErr(c, fmt.Errorf("系统用户认证失败"))
		return
	}

	users := &mysql.User{
		Comid: int(companyid),
	}

	RespSucc(c, users.GetAll())
}
